package org.misalen.web.admin.controllers;

import org.misalen.web.admin.util.RedPacket;
import org.misalen.web.admin.util.ServerResult;
import org.misalen.web.database.model.mysql.pro.ProWebAdvertising;
import org.misalen.web.database.repository.mysql.ProWebAdvertisingRepository;
import org.misalen.web.database.util.PaginationFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/pro-web-advertising")
public class ProWebAdvertisingController extends BaseController {

	@Autowired
	protected ProWebAdvertisingRepository proWebAdvertisingRepository;

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public ModelAndView list() {
		ModelAndView modelAndView = new ModelAndView("/proWebAdvertising/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost(ProWebAdvertising proWebAdvertising,
			PaginationFrom<ProWebAdvertising> pagination) {
		PaginationFrom<ProWebAdvertising> from = proWebAdvertisingRepository.findPageByBean(proWebAdvertising,
				pagination);
		return renderSuccess(from);
	}

	/**
	 * 详情
	 * 
	 * @param 主键
	 * @return 单条详情目标页
	 */
	@RequestMapping(value = "/{primaryKey}", method = { RequestMethod.GET })
	public ModelAndView details(@PathVariable String primaryKey) {
		ModelAndView modelAndView = new ModelAndView("/proWebAdvertising/details");
		modelAndView.addObject("info", proWebAdvertisingRepository.get(ProWebAdvertising.class, primaryKey));
		return modelAndView;
	}

	@RequestMapping(value = "/underReview/", method = { RequestMethod.GET })
	public ModelAndView underReview() {
		ModelAndView modelAndView = new ModelAndView("/proWebAdvertising/underReview");
		return modelAndView;
	}

	@RequestMapping(value = "/underReview/pass/{primaryKey}", method = { RequestMethod.GET })
	public @ResponseBody ServerResult underReviewPost(@PathVariable String primaryKey) {
		ProWebAdvertising proVideoInfo = proWebAdvertisingRepository.get(ProWebAdvertising.class, primaryKey);
		if (proVideoInfo.getState().equals("underReview")) {
			int[] is = RedPacket.generate(proVideoInfo.getPromotionExpenses(), proVideoInfo.getPromotionNumber(), 1,
					proVideoInfo.getPromotionExpenses());
			if(is!=null){
				proVideoInfo.setState("generalize");
				proWebAdvertisingRepository.update(proVideoInfo);
				proWebRewardRepository.saveList(is, proVideoInfo.getPrimaryKey());
			}else{
				return ServerResult.error("红包不能分配");
			}
		}
		return ServerResult.ok();
	}
}
